package com.xwj.controller.admin;

import com.xwj.data.StaticData;
import com.xwj.entity.RaMenuInfo;
import com.xwj.entity.UserInfo;
import com.xwj.entity.UserSexCountInfo;
import com.xwj.service.Impl.RaMenuServiceImpl;
import com.xwj.service.Impl.RaUserServiceImpl;
import com.xwj.service.Impl.SysUserLogServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by thinkjoy on 2017/7/10.
 */
@RestController
@RequestMapping("/admin/")
public class AdminController {

    @Autowired
    private RaMenuServiceImpl raMenuService;
    @Autowired
    private RaUserServiceImpl raUserService;
    @Autowired
    private SysUserLogServiceImpl sysUserLogService;
    private String accountid;

    //    @RequiresPermissions("user:select")
    @RequestMapping("index")
    public ModelAndView showMenus(ModelAndView modelAndView, HttpServletRequest request) {
        ServletContext context = request.getServletContext();
        accountid = (String) context.getAttribute(StaticData.ADMIN_USER_LOGIN_ATTRIBUTE);
        UserInfo userInfo = raUserService.findUserByAccountID(accountid, "0");
        if (StringUtils.isEmpty(userInfo.getPhoto())) {
            userInfo.setPhoto("/static/image/photo.jpg");
        }
        List<RaMenuInfo> raMenuInfos = raMenuService.findItemMenu();
        modelAndView.addObject("menus", raMenuInfos);
        modelAndView.addObject("userinfo", userInfo);
        modelAndView.setViewName("admin/index");
        return modelAndView;
    }

    @RequestMapping("adminlist")
    public ModelAndView admincharts(ModelAndView modelAndView) {
        modelAndView.setViewName("/admin/adminlist");
        return modelAndView;
    }


    /**
     * 男女比例
     *
     * @return
     */
    @RequestMapping(value = "adminlist/sexdata")
    public List<UserSexCountInfo> sexData() {
        List<UserInfo> userInfos = raUserService.findAllUser();
        List<UserSexCountInfo> userSexCountInfoList = new ArrayList<>();
        int count = 0;
        for (UserInfo userInfo : userInfos) {
            if (userInfo.getSex().equals("男")) {
                count++;
            }
        }
        UserSexCountInfo userSexCountInfo = new UserSexCountInfo("男", count);
        UserSexCountInfo userSexCountInfo1 = new UserSexCountInfo("女", userInfos.size() - count);
        userSexCountInfoList.add(userSexCountInfo);
        userSexCountInfoList.add(userSexCountInfo1);
        return userSexCountInfoList;
    }

    /**
     * 当前用户7天内的登录次数
     *
     * @return
     */
    @RequestMapping(value = "adminlist/usercountdata")
    public String CurrentUserLoginData() {
        String result = sysUserLogService.findSevenDayLogByAccountid(accountid);
        return result;
    }

}
